package zozopo.user;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ModifyPasswordAction extends ActionSupport implements Action,SessionAware{
	//传入参数
	private String usermail;
	private String password;
	private String newpassword;
	private String comfirmnewpassword;
	
	//传出参数
	private String operationResult;
	
	
	


	//
	private Map<Object, Object> session;
	
	public String execute(){
		UserTB user = (UserTB)session.get("userInfo"); 

		if(user == null){
			return NONE;
		}
		
		
		if(!newpassword.equals(comfirmnewpassword)){
			operationResult = "输入两次密码不一致!";
			return SUCCESS;
		}
		
		
		UserOS userOS = new UserOS();
		try {
			if(userOS.isRealUserByEmail(user.getUser_email(), password)){
				userOS.updatePassowrdByEmial(user.getUser_email(), newpassword);
				operationResult = "修改成功";
			}else{
				operationResult = "输入密码错误!";
			}
			
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			userOS.releaseConnection();
		}
		
		userOS.releaseConnection();
		
		return SUCCESS;
		
	}
	
	
	
	
	
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	

	public String getUsermail() {
		return usermail;
	}


	public void setUsermail(String usermail) {
		this.usermail = usermail;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getNewpassword() {
		return newpassword;
	}


	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}


	public String getComfirmnewpassword() {
		return comfirmnewpassword;
	}


	public void setComfirmnewpassword(String comfirmnewpassword) {
		this.comfirmnewpassword = comfirmnewpassword;
	}
	
	public String getOperationResult() {
		return operationResult;
	}


	public void setOperationResult(String operationResult) {
		this.operationResult = operationResult;
	}
}
